package calendar;
import java.util.HashMap;
import java.util.Map;
import java.util.TimeZone;

import org.apache.commons.lang.StringUtils;
import org.slf4j.LoggerFactory;

import com.thoughtworks.xstream.XStream;
import com.thoughtworks.xstream.converters.basic.DateConverter;
import com.thoughtworks.xstream.io.xml.DomDriver;

public class XmlUtils {  
  
    private static final org.slf4j.Logger LOGGER = LoggerFactory.getLogger(XmlUtils.class);
  
    public static final XStreamNameCoder nameCoder = new XStreamNameCoder();  
  
    // 编码格式  
    private static final String ENCODING = "UTF-8";  
  
    // dom解析驱动  
    private static final DomDriver fixDriver = new DomDriver(ENCODING, nameCoder);  
  
    // 通用解析器  
    public static final XStream XSTREAM = new XStream(fixDriver);  
  
    // 每个接口对接一个专用解析器  
    private static final Map<String, XStream> xstreamWrapper = new HashMap<String, XStream>();  
  
    private static final String CHINA_TIME_ZONE = "Asia/Shanghai";  
  
    private static final String XML_BEGIN_STR = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>";  
  
    static {  
        // 时区处理  
        TimeZone zone = TimeZone.getTimeZone(CHINA_TIME_ZONE);  
        XSTREAM.registerConverter(new DateConverter(zone), XStream.PRIORITY_NORMAL);  
        XSTREAM.autodetectAnnotations(true);  
  
        XStream xstream;  
  
  
        // BReq申请专用  
        xstream = new XStream(fixDriver);  
        xstream.autodetectAnnotations(true);  
        xstream.alias("Root", Person.class);  
        xstreamWrapper.put("UseBreq", xstream);  
  
    }  
  
    private XmlUtils() {  
    }  
  
    /** 
     * 报文转对象 
     *  
     * @param wrapperKey 解析器获取。要传入合法的键值。为空时取默认值 
     * @param xml 待解析的xml串 
     * @return Object 
     */  
    public static Object fromXML(String wrapperKey, String xml) {  
        Object target = null;  
        XStream xstream = XSTREAM;  
        if (StringUtils.isNotEmpty(wrapperKey)) {  
            xstream = xstreamWrapper.get(wrapperKey);  
        }  
        try {  
            target = xstream.fromXML(xml);  
        } catch (RuntimeException e) {  
            LOGGER.error("解析XML错误，解析器名称【" + wrapperKey + "】报文结构：" + xml, e);  
        }  
        return target;  
    }  
  
    /** 
     * 对象转报文 
     *  
     * @param wrapperKey 解析器获取。要传入合法的键值。为空时取默认值 
     * @param obj 待转换的对象 
     * @return 
     */  
    public static String toXML(String wrapperKey, Object obj) {  
        XStream xstream = XSTREAM; 
     
        if (StringUtils.isBlank(wrapperKey)) {  
            xstream = xstreamWrapper.get(wrapperKey);  
        }  
        return xstream.toXML(obj);  
    }  
  
    /** 
     * 去掉xml多余的空白、换行符 
     */  
    public static String trimXml(String xml) {  
        String result = xml;  
        if (null != result) {  
            result = result.replaceAll(">\\s+<", "><");  
        }  
        return result;  
    }  
  
    /** 
     * xml增加开头 
     *  
     * @param xml 
     * @return 
     */  
    public static String addXmlBegin(String xml) {  
        return XML_BEGIN_STR + xml;  
    }  
  
}  